using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CYQ.Data;
using CYQ.Data.UI;
using CYQ.Data.Table;
using Microsoft.Win32;

namespace CAF.LanguageSetting
{
    public partial class Setting : Form
    {
        public string cafwx = "Server=139.217.201.126;Initial Catalog=CAFWeChatDB;User ID=sa;Password=!@34rewqasdf;";
        public Setting()
        {
            InitializeComponent();
            //AppConfig.Cache.IsAutoCache = false;
        }

        private void LanguageSetting_Load(object sender, EventArgs e)
        {
            AppConfig.Cache.IsAutoCache = false;
            ListItem listItem1 = new ListItem("0", "0");
            ListItem listItem2 = new ListItem("1", "1");
            ListItem listItem3 = new ListItem("2", "2");
            ListItem listItem4 = new ListItem("3", "3");
            cob_DISPLAYLEVEL.Items.Add(listItem1);
            cob_DISPLAYLEVEL.Items.Add(listItem2);
            cob_DISPLAYLEVEL.Items.Add(listItem3);
            cob_DISPLAYLEVEL.Items.Add(listItem4);
            if (txt_cafwx.Text != "")
            {
                cafwx = txt_cafwx.Text;
            }
            ChangeConn();
            this.dataGridView1.SetCellDoubleClick();
            this.dataGridView2.SetCellDoubleClick();
            this.dataGridView3.SetCellDoubleClick();
            dataGridView_AD_COLUMN.SetCellDoubleClick();
        }
        private void btn_changeConn_Click(object sender, EventArgs e)
        {
            if (txt_cafwx.Text != "")
            {
                cafwx = txt_cafwx.Text;
            }
            ChangeConn();
        }
        public void ChangeConn()
        {
            using (MAction action = new MAction("AD_Window", cafwx))
            {
                var data = action.SetSelectColumns("AD_WINDOW_ID", "NAME").Select().ToDataTable();
                comboBox_window.ValueMember = "AD_WINDOW_ID";
                comboBox_window.DisplayMember = "NAME";
                comboBox_window.DataSource = data;

                //var data = action.SetSelectColumns("AD_WINDOW_ID", "NAME").Select();
                //data.Bind(comboBox_window);
            }
            using (MAction action = new MAction("AD_REFERENCE", cafwx))
            {
                var data = action.SetSelectColumns("AD_REFERENCE_ID", "NAME").Select("ISACTIVE='Y'");
                MDataRow mdr = data.NewRow();
                mdr[0].Value = "-1";
                mdr[1].Value = "请选择";
                data.Rows.Insert(0, mdr);

                data.Bind(comboBox_AD_REFERENCE_VALUE_ID);
            }
        }

        private void comboBox_window_SelectedIndexChanged(object sender, EventArgs e)
        {
            var window = (DataRowView)comboBox_window.SelectedItem;
            var window_id = window.Row["AD_WINDOW_ID"];
            using (MAction action = new MAction("AD_Table", cafwx))
            {
                //var data = action.SetSelectColumns("AD_TABLE_ID", "NAME").Select("AD_WINDOW_ID=" + window_id).ToDataTable();
                //comboBox_Table.ValueMember = "AD_TABLE_ID";
                //comboBox_Table.DisplayMember = "NAME";
                //comboBox_Table.DataSource = data;
                var data = action.SetSelectColumns("AD_TABLE_ID", "NAME").Select("AD_WINDOW_ID=" + window_id);
                data.Bind(comboBox_Table);
            }
            using (MAction action = new MAction("AD_Tab", cafwx))
            {
                var data = action.SetSelectColumns("AD_TAB_ID", "NAME").Select("AD_WINDOW_ID=" + window_id + " order by SEQNO ");
                data.Bind(comboBox_Tab);
            }
            using (MAction action = new MAction("AD_Window_TRL", cafwx))
            {
                var list = action.SetSelectColumns("AD_LANGUAGE", "NAME").Select("AD_LANGUAGE='EN' AND  AD_WINDOW_ID=" + window_id);
                if (list != null && list.Rows.Count == 1)
                {
                    var row = list.Rows[0];
                    txt_window_EN.Text = row["NAME"].Value.ToString();
                }
                var list2 = action.SetSelectColumns("AD_LANGUAGE", "NAME").Select("AD_LANGUAGE='CN' AND  AD_WINDOW_ID=" + window_id);
                if (list2 != null && list2.Rows.Count == 1)
                {
                    var row = list2.Rows[0];
                    txt_window_CN.Text = row["NAME"].Value.ToString();
                }
            }
            using (MAction action = new MAction("AD_Window", cafwx))
            {
                var list = action.SetSelectColumns("AD_Window_ID", "AD_Window_UU").Select("AD_WINDOW_ID=" + window_id);
                if (list != null && list.Rows.Count == 1)
                {
                    var row = list.Rows[0];
                    txt_AD_Window_ID.Text = row["AD_Window_ID"].Value.ToString();
                    txt_AD_Window_UU.Text = row["AD_Window_UU"].Value.ToString();
                }
            }


        }

        private void comboBox_Table_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadGridData_AD_COLUMN();
        }

        private void comboBox_Tab_SelectedIndexChanged(object sender, EventArgs e)
        {
            var ADTAB = (MDataRow)comboBox_Tab.SelectedItem;
            var ADTABID = ADTAB["AD_TAB_ID"];
            using (MAction action = new MAction("AD_TAB_TRL", cafwx))
            {
                var list = action.SetSelectColumns("AD_LANGUAGE", "NAME").Select("AD_LANGUAGE='EN' AND  AD_TAB_ID=" + ADTABID);
                if (list != null && list.Rows.Count == 1)
                {
                    var row = list.Rows[0];
                    txt_tab_EN.Text = row["NAME"].Value.ToString();
                }
                var list2 = action.SetSelectColumns("AD_LANGUAGE", "NAME").Select("AD_LANGUAGE='CN' AND  AD_TAB_ID=" + ADTABID);
                if (list2 != null && list2.Rows.Count == 1)
                {
                    var row = list2.Rows[0];
                    txt_tab_CN.Text = row["NAME"].Value.ToString();
                }
            }
            using (MAction action = new MAction("AD_TAB", cafwx))
            {
                chk_tab_hasTree.Checked = false;
                chk_tab_infoTable.Checked = false;
                chk_tab_ISACTIVE.Checked = false;
                radio_tab_IsOneReject.Checked = false;
                chk_tab_IsSelectionOnlyOne.Checked = false;
                radio_tab_IsSelectionMultiple.Checked = false;
                chk_tab_AfterReject_HasAction.Checked = false;
                var list = action.SetSelectColumns("HASTREE", "ISINFOTAB", "SEQNO", "WHERECLAUSE", "AD_TAB_ID", "TabLevel", "ISACTIVE", "ORDERBYCLAUSE", "IsOneReject", "PageSize", "IsSelectionOnlyOne", "IsSelectionMultiple", "AfterReject_HasAction").Select("AD_TAB_ID=" + ADTABID);
                if (list != null && list.Rows.Count == 1)
                {
                    var row = list.Rows[0];
                    if (row["HASTREE"].Value.ToString().ToUpper().Equals("Y"))
                    {
                        chk_tab_hasTree.Checked = true;
                    }
                    if (row["ISINFOTAB"].Value.ToString().ToUpper().Equals("Y"))
                    {
                        chk_tab_infoTable.Checked = true;
                    }
                    if (row["ISACTIVE"].Value.ToString().ToUpper().Equals("Y"))
                    {
                        chk_tab_ISACTIVE.Checked = true;
                    }
                    if (row["IsOneReject"].Value!=null && row["IsOneReject"].Value.ToString().Equals("1"))
                    {
                        radio_tab_IsOneReject.Checked = true;
                    }
                    if (row["IsSelectionOnlyOne"].Value != null && row["IsSelectionOnlyOne"].Value.ToString().Equals("1"))
                    {
                        chk_tab_IsSelectionOnlyOne.Checked = true;
                    }
                    if (row["IsSelectionMultiple"].Value != null && row["IsSelectionMultiple"].Value.ToString().Equals("1"))
                    {
                        radio_tab_IsSelectionMultiple.Checked = true;
                    }
                    if (row["AfterReject_HasAction"].Value != null && row["AfterReject_HasAction"].Value.ToString().Equals("1"))
                    {
                        chk_tab_AfterReject_HasAction.Checked = true;
                    }
                    txt_tab_id.Text = row["AD_TAB_ID"].Value != null ? row["AD_TAB_ID"].Value.ToString() : "";
                    txt_tab_WHERECLAUSE.Text = row["WHERECLAUSE"].Value != null ? row["WHERECLAUSE"].Value.ToString() : "";
                    txt_ORDERBYCLAUSE.Text = row["ORDERBYCLAUSE"].Value != null ? row["ORDERBYCLAUSE"].Value.ToString() : "";
                    txt_TabLevel.Text = row["TabLevel"].Value != null ? row["TabLevel"].Value.ToString() : "";
                    txt_tab_seqno.Text = row["SEQNO"].Value != null ? row["SEQNO"].Value.ToString() : "";
                    txt_tab_PageSize.Text = row["PageSize"].Value != null ? row["PageSize"].Value.ToString() : "";
                }
            }

            LoadGridData();
        }

        private void LoadGridData()
        {
            var ADTAB = (MDataRow)comboBox_Tab.SelectedItem;
            var ADTABID = ADTAB["AD_TAB_ID"];
            using (MAction action = new MAction("AD_FIELD", cafwx))
            {
                var data = action.SetSelectColumns("AD_FIELD_ID", "NAME", "DISPLAYLEVEL", "SEQNO").Select("AD_TAB_ID=" + ADTABID + " order by SEQNO");
                data.Bind(dataGridView1);
            }
        }
        private void loadGridData_AD_COLUMN()
        {
            var ADTAB = (MDataRow)comboBox_Table.SelectedItem;
            var ADTABID = ADTAB["AD_TABLE_ID"];
            using (MAction action = new MAction("AD_COLUMN", cafwx))
            {
                var data = action.SetSelectColumns("AD_COLUMN_ID", "FIELDLENGTH", "SEQNO", "COLUMNNAME").Select("AD_TABLE_ID=" + ADTABID + " order by SEQNO");
                data.Bind(dataGridView_AD_COLUMN);
            }

        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            var ADFIELD = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            try
            {
                int aaa = Convert.ToInt32(ADFIELD);
                txtId.Text = ADFIELD;
                txt_ID.Text = ADFIELD;
                cnMsg.Text = "";
                enMsg.Text = "";
                using (MAction action = new MAction("AD_FIELD_TRL", cafwx))
                {
                    var list = action.Select("AD_LANGUAGE='CN' AND AD_FIELD_ID=" + ADFIELD);
                    if (list != null && list.Rows.Count == 1)
                    {
                        var row = list.Rows[0];
                        txtCN.Text = row["NAME"].Value.ToString();
                        txtCN.ReadOnly = false;
                    }
                    else
                    {
                        txtCN.Text = "";
                        txtCN.ReadOnly = true;
                    }

                    var list2 = action.Select("AD_LANGUAGE='EN' AND AD_FIELD_ID=" + ADFIELD);
                    if (list2 != null && list2.Rows.Count == 1)
                    {
                        var row = list2.Rows[0];
                        txtEN.Text = row["NAME"].Value.ToString();
                        txtEN.ReadOnly = false;
                    }
                    else
                    {
                        txtEN.Text = "";
                        txtEN.ReadOnly = true;
                    }
                    if (txtEN.ReadOnly && txtCN.ReadOnly)
                    {
                        btnAdd.Visible = true;
                        btnupdate.Visible = false;

                        txtCN.ReadOnly = false;
                        txtEN.ReadOnly = false;
                    }
                    else
                    {
                        btnAdd.Visible = false;
                        btnupdate.Visible = true;
                    }
                }
                using (MAction action = new MAction("AD_FIELD", cafwx))
                {
                    var list = action.Select("AD_FIELD_ID=" + ADFIELD);
                    if (list != null && list.Rows.Count == 1)
                    {
                        var row = list.Rows[0];
                        txt_DISPLAYLOGIC.Text = row["DISPLAYLOGIC"].Value != null ? row["DISPLAYLOGIC"].Value.ToString() : "";
                        txt_SEQNO.Text = row["SEQNO"].Value != null ? row["SEQNO"].Value.ToString() : "";
                        var v = row["DISPLAYLEVEL"].Value != null ? row["DISPLAYLEVEL"].Value.ToString() : "";
                        if (!String.IsNullOrEmpty(v))
                        {
                            cob_DISPLAYLEVEL.SelectedIndex = Convert.ToInt32(v);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void dataGridView_AD_COLUMN_SelectionChanged(object sender, EventArgs e)
        {
            var id = dataGridView_AD_COLUMN.CurrentRow.Cells[0].Value.ToString();
            try
            {
                int aaa = Convert.ToInt32(id);

                txt_AD_COLUMN_ID.Text = id;
                using (MAction action = new MAction("AD_COLUMN", cafwx))
                {
                    var list = action.Select("AD_COLUMN_ID=" + id);
                    if (list != null && list.Rows.Count == 1)
                    {
                        var row = list.Rows[0];
                        txt_AD_COLUMN_FieldLength.Text = row["FieldLength"].Value != null ? row["FieldLength"].Value.ToString() : "";
                        txt_AD_COLUMN_SEQNO.Text = row["SEQNO"].Value != null ? row["SEQNO"].Value.ToString() : "";
                        var v = row["AD_REFERENCE_VALUE_ID"].Value != null ? row["AD_REFERENCE_VALUE_ID"].Value.ToString() : "-1";
                        var I = 0;
                        for (int i = 0; i < comboBox_AD_REFERENCE_VALUE_ID.Items.Count; i++)
                        {
                            MDataRow mdr = (MDataRow)comboBox_AD_REFERENCE_VALUE_ID.Items[i];
                            if (mdr[0].Value.Equals(row["AD_REFERENCE_VALUE_ID"].Value))
                            {
                                I = i;
                            }
                        }
                        comboBox_AD_REFERENCE_VALUE_ID.SelectedIndex = I;
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {

            bool result = false;
            if (!String.IsNullOrEmpty(txtEN.Text))
            {
                using (MAction action = new MAction("AD_FIELD_TRL", cafwx))
                {
                    action.Set("Name", txtEN.Text);
                    result = action.Update("AD_LANGUAGE='EN' AND AD_FIELD_ID=" + txtId.Text, true);
                    if (result)
                    {
                        enMsg.Text = "success";
                    }
                    else
                    {
                        enMsg.Text = "success";
                    }
                }
            }
            result = false;
            if (!String.IsNullOrEmpty(txtCN.Text))
            {
                using (MAction action = new MAction("AD_FIELD_TRL", cafwx))
                {
                    action.Set("Name", txtCN.Text);
                    result = action.Update("AD_LANGUAGE='CN' AND AD_FIELD_ID=" + txtId.Text, true);
                    if (result)
                    {
                        cnMsg.Text = "success";
                    }
                    else
                    {
                        cnMsg.Text = "fail";
                    }
                }
            }
        }
        private void btn_update_AD_COLUMN_Click(object sender, EventArgs e)
        {
            bool result = false;
            using (MAction action = new MAction("AD_COLUMN", cafwx))
            {


                var AD_REFERENCE = (MDataRow)comboBox_AD_REFERENCE_VALUE_ID.SelectedItem;
                var id = AD_REFERENCE["AD_REFERENCE_ID"].StringValue;

                if (id == "-1")
                    action.Set("AD_REFERENCE_VALUE_ID", DBNull.Value);
                else
                    action.Set("AD_REFERENCE_VALUE_ID", id.ToString());


                action.Set("FieldLength", txt_AD_COLUMN_FieldLength.Text);
                if (!String.IsNullOrEmpty(txt_AD_COLUMN_SEQNO.Text))
                    action.Set("SEQNO", txt_AD_COLUMN_SEQNO.Text);
                result = action.Update("AD_COLUMN_ID=" + txt_AD_COLUMN_ID.Text, true);

                if (result)
                {
                    update_AD_COLUMN_Msg.Text = "success";
                }
                else
                {
                    update_AD_COLUMN_Msg.Text = "success";
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            MessageBoxButtons mess = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("Delete?", "Info", mess);
            if (dr == DialogResult.OK)
            {
                using (MAction action = new MAction("AD_FIELD_TRL", cafwx))
                {
                    if (action.Delete(" AD_FIELD_ID=" + txtId.Text))
                    {
                        LoadGridData();
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool result1 = false, result2 = false;
            using (MAction action = new MAction("AD_FIELD_TRL", cafwx))
            {
                if (!String.IsNullOrEmpty(txtEN.Text))
                {
                    action.BeginTransation();
                    action.Set("AD_FIELD_ID", txtId.Text);
                    action.Set("AD_LANGUAGE", "EN");
                    action.Set("AD_CLIENT_ID", "0");
                    action.Set("AD_ORG_ID", "0");
                    action.Set("ISACTIVE", "Y");
                    action.Set("CREATED", DateTime.Now);
                    action.Set("CREATEDBY", "0");
                    action.Set("UPDATED", DateTime.Now);
                    action.Set("UPDATEDBY", "0");
                    action.Set("Name", txtEN.Text);
                    action.Set("ISTRANSLATED", "Y");
                    action.Set("AD_FIELD_TRL_UU", Guid.NewGuid());
                    result1 = action.Insert(true);
                    if (result1)
                    {
                        enMsg.Text = "success";
                    }
                    else
                    {
                        enMsg.Text = "fail";
                    }
                }
                if (!String.IsNullOrEmpty(txtCN.Text))
                {
                    action.BeginTransation();
                    action.Set("AD_FIELD_ID", txtId.Text);
                    action.Set("AD_LANGUAGE", "CN");
                    action.Set("AD_CLIENT_ID", "0");
                    action.Set("AD_ORG_ID", "0");
                    action.Set("ISACTIVE", "Y");
                    action.Set("CREATED", DateTime.Now);
                    action.Set("CREATEDBY", "0");
                    action.Set("UPDATED", DateTime.Now);
                    action.Set("UPDATEDBY", "0");
                    action.Set("Name", txtCN.Text);
                    action.Set("ISTRANSLATED", "Y");
                    action.Set("AD_FIELD_TRL_UU", Guid.NewGuid());
                    result2 = action.Insert(true);
                    if (result2)
                    {
                        cnMsg.Text = "success";
                    }
                    else
                    {
                        cnMsg.Text = "fail";
                    }
                }
                action.EndTransation();
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var levels = (ListItem)cob_DISPLAYLEVEL.SelectedItem;
            var level = levels.Key;
            using (MAction action = new MAction("AD_FIELD", cafwx))
            {
                action.Set("DISPLAYLEVEL", level);
                if (!String.IsNullOrEmpty(txt_DISPLAYLOGIC.Text))
                    action.Set("DISPLAYLOGIC", txt_DISPLAYLOGIC.Text);
                else action.Set("DISPLAYLOGIC", DBNull.Value);
                if (!String.IsNullOrEmpty(txt_SEQNO.Text))
                    action.Set("SEQNO", txt_SEQNO.Text);
                var result = action.Update("AD_FIELD_ID=" + txt_ID.Text, true);
                if (result)
                {
                    AD_FIELD_Msg.Text = "success";
                }
                else
                {
                    AD_FIELD_Msg.Text = "success";
                }
            }
        }

        private void groupBox4_Enter(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {
            if (e.Column.Name == "SEQNO" || e.Column.Name == "DISPLAYLEVEL")
            {
                e.SortResult = (Convert.ToInt32(e.CellValue1) - Convert.ToInt32(e.CellValue2) > 0) ? 1 : (Convert.ToInt32(e.CellValue1) - Convert.ToInt32(e.CellValue2) < 0) ? -1 : 0;
            }
            e.Handled = true;
        }

        private void btn_window_Click(object sender, EventArgs e)
        {
            var window = (DataRowView)comboBox_window.SelectedItem;
            var window_id = window.Row["AD_WINDOW_ID"];
            bool result = false;
            if (!String.IsNullOrEmpty(txt_window_CN.Text))
            {
                using (MAction action = new MAction("AD_WINDOW_TRL", cafwx))
                {
                    action.Set("Name", txt_window_CN.Text);
                    result = action.Update("AD_LANGUAGE='CN' AND AD_WINDOW_ID=" + window_id, true);
                    if (result)
                    {
                        //enMsg.Text = "success";
                    }
                    else
                    {
                        //enMsg.Text = "success";
                    }
                }
            }
            result = false;
            if (!String.IsNullOrEmpty(txt_window_EN.Text))
            {
                using (MAction action = new MAction("AD_WINDOW_TRL", cafwx))
                {
                    action.Set("Name", txt_window_EN.Text);
                    result = action.Update("AD_LANGUAGE='EN' AND AD_WINDOW_ID=" + window_id, true);
                    if (result)
                    {
                        //cnMsg.Text = "success";
                    }
                    else
                    {
                        //cnMsg.Text = "fail";
                    }
                }
            }
        }

        private void btn_tab_Click(object sender, EventArgs e)
        {
            var ADTAB = (MDataRow)comboBox_Tab.SelectedItem;
            var ADTABID = ADTAB["AD_TAB_ID"];

            bool result = false;
            if (!String.IsNullOrEmpty(txt_tab_CN.Text))
            {
                using (MAction action = new MAction("AD_TAB_TRL", cafwx))
                {
                    action.Set("Name", txt_tab_CN.Text);
                    result = action.Update("AD_LANGUAGE='CN' AND AD_TAB_ID=" + ADTABID, true);
                    if (result)
                    {
                        //enMsg.Text = "success";
                    }
                    else
                    {
                        //enMsg.Text = "success";
                    }
                }
            }
            result = false;
            if (!String.IsNullOrEmpty(txt_tab_EN.Text))
            {
                using (MAction action = new MAction("AD_TAB_TRL", cafwx))
                {
                    action.Set("Name", txt_tab_EN.Text);
                    result = action.Update("AD_LANGUAGE='EN' AND AD_TAB_ID=" + ADTABID, true);
                    if (result)
                    {
                        //cnMsg.Text = "success";
                    }
                    else
                    {
                        //cnMsg.Text = "fail";
                    }
                }
            }
            using (MAction action = new MAction("AD_TAB", cafwx))
            {
                action.Set("HASTREE", chk_tab_hasTree.Checked ? "Y" : "N");
                action.Set("ISINFOTAB", chk_tab_infoTable.Checked ? "Y" : "N");
                action.Set("ISACTIVE", chk_tab_ISACTIVE.Checked ? "Y" : "N");
                action.Set("SEQNO", txt_tab_seqno.Text);
                action.Set("TabLevel", txt_TabLevel.Text);

                action.Set("IsOneReject", radio_tab_IsOneReject.Checked ? 1 : 0);
                action.Set("IsSelectionOnlyOne", chk_tab_IsSelectionOnlyOne.Checked ? 1 : 0);
                action.Set("IsSelectionMultiple", radio_tab_IsSelectionMultiple.Checked ? 1 : 0);
                action.Set("AfterReject_HasAction", chk_tab_AfterReject_HasAction.Checked ? 1 : 0);

                if (string.IsNullOrEmpty(txt_tab_PageSize.Text))
                    action.Set("PageSize", DBNull.Value);
                else
                    action.Set("PageSize", txt_tab_PageSize.Text);

                action.Set("WHERECLAUSE", txt_tab_WHERECLAUSE.Text);
                action.Set("ORDERBYCLAUSE", txt_ORDERBYCLAUSE.Text);
                result = action.Update("AD_TAB_ID=" + ADTABID, true);
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            var tableid = (int)tablecmb.SelectedValue;
            var tabid = (int)tabcmb.SelectedValue;
            var sql = @"INSERT INTO [dbo].[AD_FIELD]          ([AD_CLIENT_ID],[AD_ORG_ID],[ISACTIVE],[CREATED],[CREATEDBY],[UPDATED],[UPDATEDBY],[NAME],[DESCRIPTION],[HELP],[ISCENTRALLYMAINTAINED],[AD_TAB_ID],[AD_COLUMN_ID],[AD_FIELDGROUP_ID],[ISDISPLAYED],[DISPLAYLOGIC],[DISPLAYLENGTH],[ISREADONLY],[SEQNO],[SORTNO],[ISSAMELINE],[ISHEADING],[ISFIELDONLY],[ISENCRYPTED],[ENTITYTYPE],[OBSCURETYPE],[AD_REFERENCE_ID],[ISMANDATORY],[INCLUDED_TAB_ID],[DEFAULTVALUE],[AD_REFERENCE_VALUE_ID],[AD_VAL_RULE_ID],[INFOFACTORYCLASS],[AD_FIELD_UU],[ISALLOWCOPY],[SEQNOGRID],[ISDISPLAYEDGRID],[XPOSITION],[NUMLINES],[COLUMNSPAN],[ISQUICKENTRY],[ISUPDATEABLE],[ISALWAYSUPDATEABLE],[MANDATORYLOGIC],[READONLYLOGIC],[ISTOOLBARBUTTON],[ISADVANCEDFIELD],[ISDEFAULTFOCUS],[VFORMAT],[AD_LABELSTYLE_ID],[AD_FIELDSTYLE_ID],DISPLAYLEVEL)             
            select  0,0,'Y',GETDATE(),0,GETDATE(),0,NAME,[DESCRIPTION],HELP,N'N',37,AD_COLUMN_ID,NULL,N'Y',NULL,NULL,N'Y',SEQNO,SEQNO            ,N'N',N'N',N'N',N'N',ENTITYTYPE,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NEWID(),NULL,NULL,NULL,NULL,NULL,NULL,N'N',NULL,NULL,NULL,NULL,NULL,NULL,N'N',NULL,NULL,NULL,0            FROM AD_COLUMN WHERE AD_TABLE_ID='" + tableid + "' ;";
            MProc proc = new MProc(sql, cafwx);
            proc.ExeNonQuery();
            var sql1 = @" insert into AD_FIELD_TRL ([AD_FIELD_ID],[AD_LANGUAGE],[AD_CLIENT_ID],[AD_ORG_ID],[ISACTIVE],[CREATED],[CREATEDBY],[UPDATED],[UPDATEDBY],[NAME],[DESCRIPTION],[HELP],[ISTRANSLATED],[AD_FIELD_TRL_UU])           select AD_FIELD_ID,'EN',0,0,'Y',GETDATE(),0,GETDATE(),0,Name,[DESCRIPTION],NULL,'Y',NEWID()    FROM AD_FIELD WHERE NAME NOT LIKE '%CN'  AND AD_TAB_ID IN='" + tabid + "' ;";
            MProc proc1 = new MProc(sql1, cafwx);
            proc1.ExeNonQuery();
            var sql2 = @" insert into AD_FIELD_TRL ([AD_FIELD_ID],[AD_LANGUAGE],[AD_CLIENT_ID],[AD_ORG_ID],[ISACTIVE],[CREATED],[CREATEDBY],[UPDATED],[UPDATEDBY],[NAME],[DESCRIPTION],[HELP],[ISTRANSLATED],[AD_FIELD_TRL_UU])            select AD_FIELD_ID,'CN',0,0,'Y',GETDATE(),0,GETDATE(),0,Name+N'中文',[DESCRIPTION],NULL,'Y',NEWID()    FROM AD_FIELD  WHERE NAME NOT LIKE '%EN' AND AD_TAB_ID IN '" + tabid + "' ;";
            MProc proc2 = new MProc(sql2, cafwx);
            proc2.ExeNonQuery();
        }

        private void btn_url_Click_Click(object sender, EventArgs e)
        {
            //需要管理员权限  app.manifest  <requestedExecutionLevel level="highestAvailable" uiAccess="false" />
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
            if (key != null)
            {
                key.SetValue("CAF.LanguageSetting.exe", 11001, RegistryValueKind.DWord);
                key.SetValue("CAF.LanguageSetting.vshost.exe", 11001, RegistryValueKind.DWord);//调试运行需要加上，否则不起作用
            }

            key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION", true);
            if (key != null)
            {
                key.SetValue("CAF.LanguageSetting.exe", 11001, RegistryValueKind.DWord);
                key.SetValue("CAF.LanguageSetting.vshost.exe", 11001, RegistryValueKind.DWord);//调试运行需要加上，否则不起作用
            }

            webBrowser1.Url = new Uri(txt_url.Text);
        }

        private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            txt_url.Text = webBrowser1.Url.AbsoluteUri;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            using (MAction action = new MAction("AD_REFERENCE", cafwx))
            {
                var data = action.SetSelectColumns("AD_REFERENCE_ID", "NAME").Select("ISACTIVE='Y'");
                MDataRow mdr = data.NewRow();
                mdr[0].Value = "-1";
                mdr[1].Value = "请选择";
                data.Rows.Insert(0, mdr);

                data.Bind(comboBox_AD_REFERENCE_VALUE_ID);
            }
        }

        private void AD_REFERENCE_Enter(object sender, EventArgs e)
        {

            using (MAction action = new MAction("AD_REFERENCE", cafwx))
            {
                var data = action.SetSelectColumns("AD_REFERENCE_ID", "NAME").Select("ISACTIVE='Y'");
                MDataRow mdr = data.NewRow();
                mdr[0].Value = "-1";
                mdr[1].Value = "请选择";
                data.Rows.Insert(0, mdr);

                data.Bind(AD_REFERENCEGrid);
            }
        }

        private void AD_REFERENCEGrid_SelectionChanged(object sender, EventArgs e)
        {
            var AD_REFERENCE_ID = AD_REFERENCEGrid.CurrentRow.Cells[0].Value.ToString();
            try
            {
                int AD_REFERENCE_IDValue = Convert.ToInt32(AD_REFERENCE_ID);
                using (MAction action = new MAction("AD_REF_LIST", cafwx))
                {
                    var list = action.SetSelectColumns("AD_REF_LIST_ID", "NAME", "VALUE").Select("AD_REFERENCE_ID=" + AD_REFERENCE_IDValue);
                    if (list != null && list.Rows.Count > 0)
                    {
                        list.Bind(AD_REFERENCE_Detail_Grid);
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void AD_REFERENCE_Detail_Grid_SelectionChanged(object sender, EventArgs e)
        {
            var AD_REF_LIST_ID = AD_REFERENCE_Detail_Grid.CurrentRow.Cells[0].Value.ToString();
            var NAME = AD_REFERENCE_Detail_Grid.CurrentRow.Cells[1].Value.ToString();
            var VALUE = AD_REFERENCE_Detail_Grid.CurrentRow.Cells[2].Value.ToString();
            try
            {
                int AD_REF_LIST_IDValue = Convert.ToInt32(AD_REF_LIST_ID);
                using (MAction action = new MAction("AD_REF_LIST_TRL", cafwx))
                {
                    var list = action.SetSelectColumns("AD_LANGUAGE", "AD_REF_LIST_ID", "NAME").Select("AD_REF_LIST_ID=" + AD_REF_LIST_IDValue);
                    foreach (var AD_REF_LIST_TRL in list.Rows)
                    {
                        if (AD_REF_LIST_TRL.ItemArray[0].ToString() == "CN")
                        {
                            AD_REFERENCE_Detail_CN.Text = AD_REF_LIST_TRL.ItemArray[2].ToString();
                        }
                        if (AD_REF_LIST_TRL.ItemArray[0].ToString() == "EN")
                        {
                            AD_REFERENCE_Detail_EN.Text = AD_REF_LIST_TRL.ItemArray[2].ToString();
                        }
                    }

                    AD_REFERENCE_Detail_Name.Text = NAME;
                    AD_REFERENCE_Detail_Value.Text = VALUE;
                    AD_REFERENCE_Detail_Value_bak.Text = VALUE;
                }
            }
            catch (Exception)
            {

            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var AD_REF_LIST_ID = AD_REFERENCE_Detail_Grid.CurrentRow.Cells[0].Value.ToString();
            using (MAction action = new MAction("AD_REF_LIST_TRL", cafwx))
            {
                action.Set("Name", AD_REFERENCE_Detail_EN.Text);
                var result = action.Update("AD_LANGUAGE='EN' AND AD_REF_LIST_ID=" + AD_REF_LIST_ID, true);
                if (result)
                {
                    this.Text = "Success";
                }
                else
                {
                    enMsg.Text = "Error";
                }
                action.Set("Name", AD_REFERENCE_Detail_CN.Text);
                result = action.Update("AD_LANGUAGE='CN' AND AD_REF_LIST_ID=" + AD_REF_LIST_ID, true);
                if (result)
                {
                    this.Text = "Success";
                }
                else
                {
                    enMsg.Text = "Error";
                }
            }
            var AD_REFERENCE_ID = AD_REFERENCEGrid.CurrentRow.Cells[0].Value.ToString();
            using (MAction action = new MAction("AD_REF_LIST", cafwx))
            {

                action.Set("Name", AD_REFERENCE_Detail_Name.Text);
                action.Set("VALUE", AD_REFERENCE_Detail_Value.Text);
                var result = action.Update("VALUE='" + AD_REFERENCE_Detail_Value_bak.Text + "' AND AD_REFERENCE_ID=" + AD_REFERENCE_ID, true);
                if (result)
                {
                    AD_REFERENCE_Detail_Value_bak.Text = "";
                    this.Text = "Success";
                }
                else
                {
                    enMsg.Text = "Error";
                }
            }
        }

        private void AD_REFERENCEADD_Click(object sender, EventArgs e)
        {

            var AD_REFERENCE_ID = AD_REFERENCEGrid.CurrentRow.Cells[0].Value.ToString();
            var AD_REFERENCEADDNAMEValue = AD_REFERENCEADDNAME.Text;

            if (!String.IsNullOrEmpty(AD_REFERENCEADDNAMEValue))
            {
                var sql = @"INSERT INTO AD_REFERENCe	 
	   SELECT AD_CLIENT_ID,
       AD_ORG_ID,
       ISACTIVE,
       GETDATE(),
       CREATEDBY,
       GETDATE(),
       UPDATEDBY,
       '" + AD_REFERENCEADDNAMEValue + @"',
       DESCRIPTION,
       HELP,
       VALIDATIONTYPE,
       VFORMAT,
       ENTITYTYPE,
       ISORDERBYVALUE,
       NEWID() FROM dbo.AD_REFERENCE WHERE AD_REFERENCE_ID=" + AD_REFERENCE_ID;
                using (MProc proc = new MProc(sql, cafwx))
                {
                    proc.BeginTransation();
                    var result1 = proc.ExeNonQuery();
                    if (result1 > 0)
                    {
                        enMsg.Text = "success";
                    }
                    else
                    {
                        enMsg.Text = "fail";
                    }

                    proc.EndTransation();
                }
            }
            //            AD_REFERENCEGrid.
            //            AD_REFERENCEADD
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var AD_REFERENCE_ID = AD_REFERENCEGrid.CurrentRow.Cells[0].Value.ToString();
            var sql = @"delete from AD_REFERENCe  WHERE AD_REFERENCE_ID=" + AD_REFERENCE_ID
            + @" delete from AD_REF_LIST_TRL where AD_REF_LIST_ID in (select AD_REF_LIST_ID from AD_REF_LIST where AD_REFERENCE_ID=" + AD_REFERENCE_ID + ")"
            + @" delete from AD_REF_LIST where AD_REFERENCE_ID=" + AD_REFERENCE_ID;
            using (MProc proc = new MProc(sql, cafwx))
            {
                proc.BeginTransation();
                var result1 = proc.ExeNonQuery();
                if (result1 > 0)
                {
                    enMsg.Text = "success";
                }
                else
                {
                    enMsg.Text = "fail";
                }

                proc.EndTransation();
            }
        }


    }
    #region MyRegion
    static public class DataViewEx
    {

        public static void SetCellDoubleClick(this DataGridView dataGridView)
        {
            dataGridView.CellDoubleClick += dataGridView1_CellDoubleClick;
        }
        static void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var dataGridView = (DataGridView)sender;
            Clipboard.Clear();
            Clipboard.SetDataObject(dataGridView.CurrentCell.Value, true);
        }
    }
    public class ListItem
    {

        private string _key = string.Empty;

        private string _value = string.Empty;

        public ListItem(string pKey, string pValue)
        {

            _key = pKey;

            _value = pValue;

        }

        public override string ToString()
        {

            return this._value;

        }

        public string Key
        {

            get
            {

                return this._key;

            }

            set
            {

                this._key = value;

            }

        }

        public string Value
        {

            get
            {

                return this._value;

            }

            set
            {

                this._value = value;

            }

        }

    }
    #endregion
}
